package com.scalar.chiptrack.sales.actions.quote;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.sales.dao.quote.QuotationDAO;
import com.scalar.chiptrack.sales.forms.quote.QuotePopupFieldsForm;
import com.scalar.chiptrack.sales.QuoteType;
import com.scalar.chiptrack.sales.QuotePopupField;
import com.scalar.chiptrack.sales.QuoteApplicationType;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * User: Sujan
 * Date: Jun 14, 2007
 * Time: 4:37:14 PM
 */
public class QuotePopupFieldsAction extends ChipTrackAction
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);
        if (user == null)
        {
            request.setAttribute ( "isSessionExpired", "true" );
            return mapping.findForward ("success");
        }
        QuotePopupFieldsForm quotePopupFieldsForm = (QuotePopupFieldsForm)form;
        String action = request.getParameter ( "action" );
        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
        if ( "loadQuoteType".equalsIgnoreCase( action ) )
        {
            ArrayList quoteTypes = getQuoteTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteType" );
        }
        else if ( "saveQuoteType".equalsIgnoreCase( action ) )
        {
            try
            {
                if ( QuotationDAO.isQuoteTypeExists( null, quotePopupFieldsForm.getFieldName() ) )
                {
                    errors.put( "Quote Type already exists", null );
                }
                else
                {
                    QuotationDAO.saveQuoteType ( null, quotePopupFieldsForm.getFieldName(), quotePopupFieldsForm.getId() );
                    info.put ( "Quote Type saved successfully", null );
                }
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not save Quote Type", null );
                e.printStackTrace();
            }
            ArrayList quoteTypes = getQuoteTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteType" );

            quotePopupFieldsForm.setFieldName( null );
            quotePopupFieldsForm.setId( 0 );
            request.setAttribute( "refreshListData", "true");
        }
        else if ( "editQuoteType".equalsIgnoreCase( action ) )
        {
            ArrayList quoteTypes = getQuoteTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteType" );
        }
        else if ( "disableQuoteType".equalsIgnoreCase( action ) )
        {
            try
            {
                QuotationDAO.updateQuoteTypeStatus( null, quotePopupFieldsForm.getId(), true );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not disable Quote Type", null );
                e.printStackTrace();
            }
            ArrayList quoteTypes = getQuoteTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteType" );
            quotePopupFieldsForm.setFieldName( null );
            quotePopupFieldsForm.setId( 0 );
            request.setAttribute( "refreshListData", "true");
        }
        else if ( "activateQuoteType".equalsIgnoreCase( action ) )
        {
            try
            {
                QuotationDAO.updateQuoteTypeStatus( null, quotePopupFieldsForm.getId(), false );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not activate Quote Type", null );
                e.printStackTrace();
            }
            ArrayList quoteTypes = getQuoteTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteType" );
            quotePopupFieldsForm.setFieldName( null );
            quotePopupFieldsForm.setId( 0 );
            request.setAttribute( "refreshListData", "true");
        }
        if ( "loadQuoteApplication".equalsIgnoreCase( action ) )
        {
            ArrayList quoteTypes = getQuoteApplicationTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteApplication" );
        }
        else if ( "saveQuoteApplication".equalsIgnoreCase( action ) )
        {
            try
            {
                if ( QuotationDAO.isQuoteApplicationTypeExists( null, quotePopupFieldsForm.getFieldName() ) )
                {
                    errors.put( "Quote Application Type already exists", null );
                }
                else
                {
                    QuotationDAO.saveQuoteApplicationType ( null, quotePopupFieldsForm.getFieldName(), quotePopupFieldsForm.getId() );
                    info.put ( "Quote Application Type saved successfully", null );
                }
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not save Quote Application Type", null );
                e.printStackTrace();
            }
            ArrayList quoteTypes = getQuoteApplicationTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteApplication" );

            quotePopupFieldsForm.setFieldName( null );
            quotePopupFieldsForm.setId( 0 );
            request.setAttribute( "refreshListData", "true");
        }
        else if ( "editQuoteApplication".equalsIgnoreCase( action ) )
        {
            ArrayList quoteTypes = getQuoteApplicationTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteApplication" );
        }
        else if ( "disableQuoteApplication".equalsIgnoreCase( action ) )
        {
            try
            {
                QuotationDAO.updateQuoteApplicationTypeStatus( null, quotePopupFieldsForm.getId(), true );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not disable Quote Application Type", null );
                e.printStackTrace();
            }
            ArrayList quoteTypes = getQuoteApplicationTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteApplication" );
            quotePopupFieldsForm.setFieldName( null );
            quotePopupFieldsForm.setId( 0 );
            request.setAttribute( "refreshListData", "true");
        }
        else if ( "activateQuoteApplication".equalsIgnoreCase( action ) )
        {
            try
            {
                QuotationDAO.updateQuoteApplicationTypeStatus( null, quotePopupFieldsForm.getId(), false );
            }
            catch ( ScalarException e )
            {
                errors.put ( "Could not activate Quote Application Type", null );
                e.printStackTrace();
            }
            ArrayList quoteTypes = getQuoteApplicationTypeFieldList( null );
            quotePopupFieldsForm.setFieldsList( quoteTypes );
            quotePopupFieldsForm.setTitle( "QuoteApplication" );
            quotePopupFieldsForm.setFieldName( null );
            quotePopupFieldsForm.setId( 0 );
            request.setAttribute( "refreshListData", "true");
        }
        return mapping.findForward ("success");
    }
    private ArrayList getQuoteTypeFieldList ( DBTransaction dbTransaction ) throws ScalarException
    {
        ArrayList quoteTypes = QuotationDAO.getQuoteTypes( dbTransaction );
        ArrayList fieldList = new ArrayList();
        int listSize = (quoteTypes==null) ? 0 : quoteTypes.size();
        for ( int i =0 ; i < listSize; i++ )
        {
            QuoteType quoteType = (QuoteType)quoteTypes.get(i);
            QuotePopupField quotePopupField = new QuotePopupField();
            quotePopupField.setName( quoteType.getQuoteTypeName() );
            quotePopupField.setId( quoteType.getQuoteTypeId() );
            quotePopupField.setDisabled( quoteType.isDisabled() );

            fieldList.add ( quotePopupField );
        }
        return fieldList;
    }
    private ArrayList getQuoteApplicationTypeFieldList ( DBTransaction dbTransaction ) throws ScalarException
    {
        ArrayList applicationTypes = QuotationDAO.getQuoteApplications( dbTransaction );
        ArrayList fieldList = new ArrayList();
        int listSize = (applicationTypes==null) ? 0 : applicationTypes.size();
        for ( int i =0 ; i < listSize; i++ )
        {
            QuoteApplicationType quoteApplicationType = (QuoteApplicationType)applicationTypes.get(i);
            QuotePopupField quotePopupField = new QuotePopupField();
            quotePopupField.setName( quoteApplicationType.getAppTypeName() );
            quotePopupField.setId( quoteApplicationType.getAppTypeId() );
            quotePopupField.setDisabled( quoteApplicationType.isDisabled() );

            fieldList.add ( quotePopupField );
        }
        return fieldList;
    }
}
